package com.uduemc.biso.module.basic.service.customer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.uduemc.biso.core.extities.center.CustomerRole;
import com.uduemc.biso.core.extities.center.custom.CustomerUserRole;
import com.uduemc.biso.core.utils.RestResult;
import com.uduemc.biso.module.basic.service.customer.service.CustomerRoleService;

@RequestMapping("/customer/role")
@RestController
public class RoleController {

	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

	@Autowired
	private CustomerRoleService customerRoleServiceImpl;

	@PostMapping("/insert")
	public RestResult insert(@Valid @RequestBody CustomerRole customerRole, BindingResult errors) {
		logger.info("insert: " + customerRole.toString());
		if (errors.hasErrors()) {
			Map<String, Object> message = new HashMap<>();
			for (FieldError fieldError : errors.getFieldErrors()) {
				String field = fieldError.getField();
				String defaultMessage = fieldError.getDefaultMessage();
				message.put(field, defaultMessage);
			}

			return RestResult.error(message);
		}
		CustomerRole data = customerRoleServiceImpl.insert(customerRole);
		return RestResult.ok(data, CustomerRole.class.toString());
	}

	@PutMapping("/update-by-id")
	public RestResult updateById(@Valid @RequestBody CustomerRole customerRole, BindingResult errors) {
		logger.info("updateById: " + customerRole.toString());
		if (errors.hasErrors()) {
			Map<String, Object> message = new HashMap<>();
			for (FieldError fieldError : errors.getFieldErrors()) {
				String field = fieldError.getField();
				String defaultMessage = fieldError.getDefaultMessage();
				message.put(field, defaultMessage);
			}

			return RestResult.error(message);
		}
		CustomerRole findOne = customerRoleServiceImpl.findOne(customerRole.getId());
		if (findOne == null) {
			return RestResult.noData();
		}
		CustomerRole data = customerRoleServiceImpl.updateById(customerRole);
		return RestResult.ok(data, CustomerRole.class.toString());
	}

	@GetMapping("/find-one/{id:\\d+}")
	public RestResult findOne(@PathVariable("id") Long id) {
		CustomerRole findOne = customerRoleServiceImpl.findOne(id);
		if (findOne == null || findOne.getId() < 1) {
			return RestResult.noData();
		}
		return RestResult.ok(findOne, CustomerRole.class.toString());
	}

	@GetMapping(value = { "/find-all" })
	public RestResult findAll(@PageableDefault(page = 0, size = 12) Pageable pageable) {
		logger.info("pageable: " + pageable.toString());
		List<CustomerRole> findAll = customerRoleServiceImpl.findAll(pageable);
		return RestResult.ok(findAll, CustomerRole.class.toString(), true);
	}

	@GetMapping("/delete-by-id/{id:\\d+}")
	public RestResult deleteById(@PathVariable("id") Long id) {
		CustomerRole findOne = customerRoleServiceImpl.findOne(id);
		if (findOne == null) {
			return RestResult.ok();
		}
		customerRoleServiceImpl.deleteById(id);
		return RestResult.ok(findOne);
	}

	@GetMapping("/find-by-customer-user-id/{id:\\d+}")
	public RestResult findById(@PathVariable Long id) {
		List<CustomerUserRole> list = customerRoleServiceImpl.findCustomerUserRoleByCustomerId(id);
		return RestResult.ok(list, CustomerUserRole.class.toString(), true);
	}
}
